package leetcode;

import java.util.Arrays;

class 移动零 {
    public static void main(String[] args) {
        int[] nums = new int[]{0, 1, 0, 3, 12};
        moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static void moveZeroes(int[] nums) {
        //思路：定义慢指针用来指向当前在哪个位置，快指针找出不是0的数
        int length = nums.length;
        int slow = 0;
        //记录非零数的个数
        int num = 0;
        for (int fast = 0; fast < length; fast++) {
            if (nums[fast] != 0) {
                nums[slow++] = nums[fast];
                num++;
            }
        }
        for (int i = num; i < length; i++) {
            nums[i] = 0;
        }
    }
}